package views

import (
	"blog_demo/common"
	"blog_demo/service"
	"errors"
	"log"
	"net/http"
	"strconv"
)

func (*HTMLApi) Index(w http.ResponseWriter, r *http.Request) {
	index := common.Template.Index

	// 数据库查询
	if err := r.ParseForm(); err != nil {
		log.Println("r.ParseForm() error", err)
		index.WriteError(w, errors.New("系统错误，请联系管理员"))
		return
	}
	// 处理分页
	pageStr := r.Form.Get("page")
	page := 1
	if pageStr == "" {
		pageStr = "1"
	}
	page, _ = strconv.Atoi(pageStr)
	// 每页显示的数量
	pageSize := 10

	hr, err := service.GetAllIndexInfo(page, pageSize)
	if err != nil {
		log.Println("Index GetAllIndexInfo error", err)
		index.WriteError(w, errors.New("系统错误，请联系管理员"))
	}
	index.WriteData(w, hr)
}
